import numpy as np

def z_score_standardize(data):
    """
    手动实现Z-score标准化
    :param data: 输入数据（一维或二维数组）
    :return: 标准化后的数据
    """
    data = np.array(data, dtype=np.float32)
    if data.ndim == 1:
        data = data.reshape(-1, 1)
    
    # 按列计算均值和标准差
    mean = data.mean(axis=0)
    std = data.std(axis=0)
    
    # 避免除零错误（若std=0，说明该特征无波动，设为1）
    std[std == 0] = 1.0
    
    standardized = (data - mean) / std
    return standardized.squeeze()

# 示例数据
# data = np.array([10, 20, 30, 40, 50])
# print(data.shape)
# standardized_data = z_score_standardize(data)
# print("原始数据:", data)
# print("标准化后:", standardized_data)  # 输出: [-1.414 -0.707  0.     0.707  1.414]（近似值）


